package com.spzx.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.service.RemoteProductService;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserBrowseHistory;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-11-01
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService
{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;

    @Autowired
    private RemoteProductService remoteProductService;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo)
    {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public Boolean register(UserInfo userInfo) {
        //根据用户名查询数据库是否存在该用户
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, userInfo.getUsername()));
        if (count>0){
            //存在,抛异常
            throw new ServiceException("用户已存在");
        }
        int insert = baseMapper.insert(userInfo);
        return insert>0;
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {
        UserInfo userInfo = baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username));
        return userInfo;
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        //创建userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        int i = baseMapper.updateById(userInfo);
        return i>0;
    }

    //判断是否收藏
    @Override
    public Boolean isCollect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //根据用户id和商品skuId查询数据库
        UserCollect userCollect = userCollectMapper.selectOne(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, userId).eq(UserCollect::getSkuId, skuId));

        return userCollect!=null;
    }
    //收藏商品
    @Override
    public Boolean collect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //创建UserCollect对象
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setSkuId(skuId);
        int insert = userCollectMapper.insert(userCollect);
        return insert>0;
    }
    //取消收藏
    @Override
    public Boolean cancelCollect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //调用带条件的删除
        int delete = userCollectMapper.delete(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, userId).eq(UserCollect::getSkuId, skuId));
        return delete>0;
    }

    @Override
    public List<ProductSku> getuserBrowseHistoryList() {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //根据用户id查询用户所有的浏览记录,获取skuid集合
        List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(new LambdaQueryWrapper<UserBrowseHistory>().eq(UserBrowseHistory::getUserId, userId).orderByDesc(UserBrowseHistory::getUpdateTime));
        List<Long> skuIdList = userBrowseHistoryList.stream().map(userBrowseHistory -> userBrowseHistory.getSkuId()).toList();
        //创建返回集合
        List<ProductSku> productSkuList=new ArrayList<>();
        skuIdList.forEach(skuId->{
            R<ProductSku> productSku = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
            //获取商品sku
            ProductSku data = productSku.getData();
            //放入集合
            productSkuList.add(data);
        });
        return productSkuList;
    }

}
